<?php

namespace app\queue\redis;

use think\facade\Db;
use Webman\RedisQueue\Consumer;
use app\common\model\Message as MessageModel;
use Webman\Push\Api;

class OptimizeTables implements Consumer
{

// 要消费的队列名
    public $queue = 'optimize_tables';

    // 连接名，对应 plugin/webman/redis-queue/redis.php 里的连接`
    public $connection = 'default';

    // 消费队列
    public function consume($data)
    {
        try {
            $name = ! empty($data['name']) ? $data['name'] : [];
            foreach ($name as $key => $row) {
                Db::execute("OPTIMIZE TABLE `{$row}`");
            }
            $adminId = ! empty($data['admin_id']) ? $data['admin_id'] : 0;
            $d       = ['type' => 1, 'text' => '数据库管理：优化表完成', 'admin_id' => $adminId];
            MessageModel::manageAdd($d);
            //数据推送
            push_webman('admin_'.$adminId, 'message', $d);
        } catch (\Exception $e) {
            var_export($e->getMessage());
        }

    }

}
